package com.jemmyee.pizza.web.controller;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.jemmyee.core.dao.Page;
import com.jemmyee.pizza.entity.CostCylinder;
import com.jemmyee.pizza.entity.CostInstall;
import com.jemmyee.pizza.entity.CostMaintain;
import com.jemmyee.pizza.entity.CostOther;
import com.jemmyee.pizza.entity.CostPipeline;
import com.jemmyee.pizza.entity.CostUnlock;
import com.jemmyee.pizza.service.ICostCylinderService;
import com.jemmyee.pizza.service.ICostInstallService;
import com.jemmyee.pizza.service.ICostMaintainService;
import com.jemmyee.pizza.service.ICostOtherService;
import com.jemmyee.pizza.service.ICostPipelineService;
import com.jemmyee.pizza.service.ICostUnlockService;
import com.jemmyee.pizza.web.Constants;

@Controller
@RequestMapping("/admin/cost")
public class CostController {

	private static final Log log = LogFactory.getLog(CostController.class);
	
	@Autowired
	private ICostInstallService<CostInstall> costInstallService ;
	@Autowired
	private ICostMaintainService<CostMaintain> costMaintainService ;
	@Autowired
	private ICostOtherService<CostOther> costOtherService ;
	@Autowired
	private ICostUnlockService<CostUnlock> costUnlockService ;
	@Autowired
	private ICostPipelineService<CostPipeline> costPipelineService ;
	@Autowired
	private ICostCylinderService<CostCylinder> costCylinderService ;
	
	@RequestMapping("/install_list/{from}/{index}")
	public ModelAndView installList(@PathVariable Integer index,@PathVariable String from,CostInstall install,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		CostInstall installBean = new CostInstall() ;
		Integer startIndex = 0 ;
		
		if ("form".equals(from)) {
			installBean = install;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, installBean);
		}else if ("page".equals(from)) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				installBean = (CostInstall) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
			//startIndex = index ;
			if(index == -1 && request.getSession().getAttribute("session_page_sys_startIndex") != null){
				startIndex = (Integer)request.getSession().getAttribute("session_page_sys_startIndex") ;
			}
			if(index != -1){
				startIndex = index ;
				request.getSession().setAttribute("session_page_sys_startIndex", index);
			}
		}else if ("init".equals(from)) {
			request.getSession().removeAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			request.getSession().removeAttribute("session_page_sys_startIndex");
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(CostInstall.class);
		
		if(installBean != null && installBean.getCustomCode() != null && !"".equals(installBean.getCustomCode())){
			dc.add(Restrictions.like("customCode", installBean.getCustomCode(),MatchMode.ANYWHERE)) ;
		}
		
		if(installBean != null && installBean.getCustomName() != null && !"".equals(installBean.getCustomName())){
			dc.add(Restrictions.like("customName", installBean.getCustomName(),MatchMode.ANYWHERE)) ;
		}
		
		Page page = costInstallService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex,null);
		
		mv.addObject("page",page);
		mv.addObject("installBean",installBean);
		mv.setViewName("/admin/cost/install_list");
		return mv ;
	}
	
	@RequestMapping("/maintain_list/{from}/{index}")
	public ModelAndView maintainList(@PathVariable Integer index,@PathVariable String from,CostMaintain maintain,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		CostMaintain maintainBean = new CostMaintain() ;
		Integer startIndex = 0 ;
		
		if ("form".equals(from)) {
			maintainBean = maintain;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, maintainBean);
		}else if ("page".equals(from)) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				maintainBean = (CostMaintain) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
			//startIndex = index ;
			if(index == -1 && request.getSession().getAttribute("session_page_sys_startIndex") != null){
				startIndex = (Integer)request.getSession().getAttribute("session_page_sys_startIndex") ;
			}
			if(index != -1){
				startIndex = index ;
				request.getSession().setAttribute("session_page_sys_startIndex", index);
			}
		}else if ("init".equals(from)) {
			request.getSession().removeAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			request.getSession().removeAttribute("session_page_sys_startIndex");
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(CostMaintain.class);
		
		if(maintainBean != null && maintainBean.getCustomCode() != null && !"".equals(maintainBean.getCustomCode())){
			dc.add(Restrictions.like("customCode", maintainBean.getCustomCode(),MatchMode.ANYWHERE)) ;
		}
		
		if(maintainBean != null && maintainBean.getCustomName() != null && !"".equals(maintainBean.getCustomName())){
			dc.add(Restrictions.like("customName", maintainBean.getCustomName(),MatchMode.ANYWHERE)) ;
		}
		
		Page page = costMaintainService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex,null);
		
		mv.addObject("page",page);
		mv.addObject("maintainBean",maintainBean);
		mv.setViewName("/admin/cost/maintain_list");
		return mv ;
	}
	
	@RequestMapping("/other_list/{from}/{index}")
	public ModelAndView otherList(@PathVariable Integer index,@PathVariable String from,CostOther other,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		CostOther otherBean = new CostOther() ;
		Integer startIndex = 0 ;
		
		if ("form".equals(from)) {
			otherBean = other;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, otherBean);
		}else if ("page".equals(from)) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				otherBean = (CostOther) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
			//startIndex = index ;
			if(index == -1 && request.getSession().getAttribute("session_page_sys_startIndex") != null){
				startIndex = (Integer)request.getSession().getAttribute("session_page_sys_startIndex") ;
			}
			if(index != -1){
				startIndex = index ;
				request.getSession().setAttribute("session_page_sys_startIndex", index);
			}
		}else if ("init".equals(from)) {
			request.getSession().removeAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			request.getSession().removeAttribute("session_page_sys_startIndex");
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(CostOther.class);
		
		if(otherBean != null && otherBean.getCustomCode() != null && !"".equals(otherBean.getCustomCode())){
			dc.add(Restrictions.like("customCode", otherBean.getCustomCode(),MatchMode.ANYWHERE)) ;
		}
		
		if(otherBean != null && otherBean.getCustomName() != null && !"".equals(otherBean.getCustomName())){
			dc.add(Restrictions.like("customName", otherBean.getCustomName(),MatchMode.ANYWHERE)) ;
		}
		
		Page page = costOtherService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex,null);
		
		mv.addObject("page",page);
		mv.addObject("otherBean",otherBean);
		mv.setViewName("/admin/cost/other_list");
		return mv ;
	}
	
	@RequestMapping("/unlock_list/{from}/{index}")
	public ModelAndView unlockList(@PathVariable Integer index,@PathVariable String from,CostUnlock unlock,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		CostUnlock unlockBean = new CostUnlock() ;
		Integer startIndex = 0 ;
		
		if ("form".equals(from)) {
			unlockBean = unlock;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, unlockBean);
		}else if ("page".equals(from)) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				unlockBean = (CostUnlock) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
			//startIndex = index ;
			if(index == -1 && request.getSession().getAttribute("session_page_sys_startIndex") != null){
				startIndex = (Integer)request.getSession().getAttribute("session_page_sys_startIndex") ;
			}
			if(index != -1){
				startIndex = index ;
				request.getSession().setAttribute("session_page_sys_startIndex", index);
			}
		}else if ("init".equals(from)) {
			request.getSession().removeAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			request.getSession().removeAttribute("session_page_sys_startIndex");
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(CostUnlock.class);
		
		if(unlockBean != null && unlockBean.getCustomCode() != null && !"".equals(unlockBean.getCustomCode())){
			dc.add(Restrictions.like("customCode", unlockBean.getCustomCode(),MatchMode.ANYWHERE)) ;
		}
		
		if(unlockBean != null && unlockBean.getCustomName() != null && !"".equals(unlockBean.getCustomName())){
			dc.add(Restrictions.like("customName", unlockBean.getCustomName(),MatchMode.ANYWHERE)) ;
		}
		
		Page page = costUnlockService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex,null);
		
		mv.addObject("page",page);
		mv.addObject("unlockBean",unlockBean);
		mv.setViewName("/admin/cost/unlock_list");
		return mv ;
	}
	
	@RequestMapping("/pipeline_list/{from}/{index}")
	public ModelAndView pipelineList(@PathVariable Integer index,@PathVariable String from,CostPipeline pipeline,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		CostPipeline pipelineBean = new CostPipeline() ;
		Integer startIndex = 0 ;
		
		if ("form".equals(from)) {
			pipelineBean = pipeline;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, pipelineBean);
		}else if ("page".equals(from)) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				pipelineBean = (CostPipeline) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
			//startIndex = index ;
			if(index == -1 && request.getSession().getAttribute("session_page_sys_startIndex") != null){
				startIndex = (Integer)request.getSession().getAttribute("session_page_sys_startIndex") ;
			}
			if(index != -1){
				startIndex = index ;
				request.getSession().setAttribute("session_page_sys_startIndex", index);
			}
		}else if ("init".equals(from)) {
			request.getSession().removeAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			request.getSession().removeAttribute("session_page_sys_startIndex");
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(CostPipeline.class);
		
		if(pipelineBean != null && pipelineBean.getCustomCode() != null && !"".equals(pipelineBean.getCustomCode())){
			dc.add(Restrictions.like("customCode", pipelineBean.getCustomCode(),MatchMode.ANYWHERE)) ;
		}
		
		if(pipelineBean != null && pipelineBean.getCustomName() != null && !"".equals(pipelineBean.getCustomName())){
			dc.add(Restrictions.like("customName", pipelineBean.getCustomName(),MatchMode.ANYWHERE)) ;
		}
		
		Page page = costPipelineService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex,null);
		
		mv.addObject("page",page);
		mv.addObject("pipelineBean",pipelineBean);
		mv.setViewName("/admin/cost/pipeline_list");
		return mv ;
	}
	
	@RequestMapping("/cylinder_list/{from}/{index}")
	public ModelAndView cylinderList(@PathVariable Integer index,@PathVariable String from,CostCylinder cylinder,HttpServletRequest request){
		ModelAndView mv=new ModelAndView();
		
		CostCylinder cylinderBean = new CostCylinder() ;
		Integer startIndex = 0 ;
		
		if ("form".equals(from)) {
			cylinderBean = cylinder;
			request.getSession().setAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN, cylinderBean);
		}else if ("page".equals(from)) {
			if (request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN) != null) {
				cylinderBean = (CostCylinder) request.getSession().getAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			}
			//startIndex = index ;
			if(index == -1 && request.getSession().getAttribute("session_page_sys_startIndex") != null){
				startIndex = (Integer)request.getSession().getAttribute("session_page_sys_startIndex") ;
			}
			if(index != -1){
				startIndex = index ;
				request.getSession().setAttribute("session_page_sys_startIndex", index);
			}
		}else if ("init".equals(from)) {
			request.getSession().removeAttribute(Constants.SESSION_PAGE_SYS_QUERY_BEAN);
			request.getSession().removeAttribute("session_page_sys_startIndex");
		}
		
		DetachedCriteria dc = DetachedCriteria.forClass(CostCylinder.class);
		
		if(cylinderBean != null && cylinderBean.getCustomCode() != null && !"".equals(cylinderBean.getCustomCode())){
			dc.add(Restrictions.like("customCode", cylinderBean.getCustomCode(),MatchMode.ANYWHERE)) ;
		}
		
		if(cylinderBean != null && cylinderBean.getCustomName() != null && !"".equals(cylinderBean.getCustomName())){
			dc.add(Restrictions.like("customName", cylinderBean.getCustomName(),MatchMode.ANYWHERE)) ;
		}
		
		Page page = costCylinderService.findPageByCriteria(dc, Constants.DEFAULT_PAGESIZE, startIndex,null);
		
		mv.addObject("page",page);
		mv.addObject("cylinderBean",cylinderBean);
		mv.setViewName("/admin/cost/cylinder_list");
		return mv ;
	}
	
	@RequestMapping("/install_to_mod/{modType}/{id}")
	public ModelAndView install_to_mod(@PathVariable String modType,@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostInstall install = null ;
		if("update".equals(modType)){
			install = costInstallService.findById(id) ;
		}else{
			install = new CostInstall() ;
		}
		
		mv.addObject("install",install);
		mv.addObject("modType",modType) ;
		
		mv.setViewName("/admin/cost/install_mod");
		
		return mv ;
	}
	
	@RequestMapping("/maintain_to_mod/{modType}/{id}")
	public ModelAndView maintain_to_mod(@PathVariable String modType,@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostMaintain maintain = null ;
		if("update".equals(modType)){
			maintain = costMaintainService.findById(id) ;
		}else{
			maintain = new CostMaintain() ;
		}
		
		mv.addObject("maintain",maintain);
		mv.addObject("modType",modType) ;
		
		mv.setViewName("/admin/cost/maintain_mod");
		
		return mv ;
	}
	
	@RequestMapping("/other_to_mod/{modType}/{id}")
	public ModelAndView other_to_mod(@PathVariable String modType,@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostOther other = null ;
		if("update".equals(modType)){
			other = costOtherService.findById(id) ;
		}else{
			other = new CostOther() ;
		}
		
		mv.addObject("other",other);
		mv.addObject("modType",modType) ;
		
		mv.setViewName("/admin/cost/other_mod");
		
		return mv ;
	}
	
	@RequestMapping("/unlock_to_mod/{modType}/{id}")
	public ModelAndView unlock_to_mod(@PathVariable String modType,@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostUnlock unlock = null ;
		if("update".equals(modType)){
			unlock = costUnlockService.findById(id) ;
		}else{
			unlock = new CostUnlock() ;
		}
		
		mv.addObject("unlock",unlock);
		mv.addObject("modType",modType) ;
		
		mv.setViewName("/admin/cost/unlock_mod");
		
		return mv ;
	}
	
	@RequestMapping("/pipeline_to_mod/{modType}/{id}")
	public ModelAndView pipeline_to_mod(@PathVariable String modType,@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostPipeline pipeline = null ;
		if("update".equals(modType)){
			pipeline = costPipelineService.findById(id) ;
		}else{
			pipeline = new CostPipeline() ;
		}
		
		mv.addObject("pipeline",pipeline);
		mv.addObject("modType",modType) ;
		
		mv.setViewName("/admin/cost/pipeline_mod");
		
		return mv ;
	}
	
	@RequestMapping("/cylinder_to_mod/{modType}/{id}")
	public ModelAndView cylinder_to_mod(@PathVariable String modType,@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostCylinder cylinder = null ;
		if("update".equals(modType)){
			cylinder = costCylinderService.findById(id) ;
		}else{
			cylinder = new CostCylinder() ;
		}
		
		mv.addObject("cylinder",cylinder);
		mv.addObject("modType",modType) ;
		
		mv.setViewName("/admin/cost/cylinder_mod");
		
		return mv ;
	}
	
	@RequestMapping("/install_mod/{modType}")
	public ModelAndView install_mod(@PathVariable String modType,CostInstall install){
		ModelAndView mv=new ModelAndView();
		
		if("update".equals(modType)){
			costInstallService.update(install) ;
		}else{
			costInstallService.save(install) ;
		}
		
		mv.addObject("location", "admin/cost/install_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/maintain_mod/{modType}")
	public ModelAndView maintain_mod(@PathVariable String modType,CostMaintain maintain){
		ModelAndView mv=new ModelAndView();
		
		if("update".equals(modType)){
			costMaintainService.update(maintain) ;
		}else{
			costMaintainService.save(maintain) ;
		}
		
		mv.addObject("location", "admin/cost/maintain_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/other_mod/{modType}")
	public ModelAndView other_mod(@PathVariable String modType,CostOther other){
		ModelAndView mv=new ModelAndView();
		
		if("update".equals(modType)){
			costOtherService.update(other) ;
		}else{
			costOtherService.save(other) ;
		}
		
		mv.addObject("location", "admin/cost/other_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/unlock_mod/{modType}")
	public ModelAndView unlock_mod(@PathVariable String modType,CostUnlock unlock){
		ModelAndView mv=new ModelAndView();
		
		if("update".equals(modType)){
			costUnlockService.update(unlock) ;
		}else{
			costUnlockService.save(unlock) ;
		}
		
		mv.addObject("location", "admin/cost/unlock_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/pipeline_mod/{modType}")
	public ModelAndView pipeline_mod(@PathVariable String modType,CostPipeline pipeline){
		ModelAndView mv=new ModelAndView();
		
		if("update".equals(modType)){
			costPipelineService.update(pipeline) ;
		}else{
			costPipelineService.save(pipeline) ;
		}
		
		mv.addObject("location", "admin/cost/pipeline_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/cylinder_mod/{modType}")
	public ModelAndView cylinder_mod(@PathVariable String modType,CostCylinder cylinder){
		ModelAndView mv=new ModelAndView();
		
		if("update".equals(modType)){
			costCylinderService.update(cylinder) ;
		}else{
			costCylinderService.save(cylinder) ;
		}
		
		mv.addObject("location", "admin/cost/cylinder_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/install_del/{id}")
	public ModelAndView install_del(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostInstall install = costInstallService.findById(id) ;
		costInstallService.delete(install) ;
		
		mv.addObject("location", "admin/cost/install_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/maintain_del/{id}")
	public ModelAndView maintain_del(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostMaintain maintain = costMaintainService.findById(id) ;
		costMaintainService.delete(maintain) ;
		
		mv.addObject("location", "admin/cost/maintain_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/other_del/{id}")
	public ModelAndView other_del(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostOther other = costOtherService.findById(id) ;
		costOtherService.delete(other) ;
		
		mv.addObject("location", "admin/cost/other_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/unlock_del/{id}")
	public ModelAndView unlock_del(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostUnlock unlock = costUnlockService.findById(id) ;
		costUnlockService.delete(unlock) ;
		
		mv.addObject("location", "admin/cost/unlock_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/pipeline_del/{id}")
	public ModelAndView pipeline_del(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostPipeline pipeline = costPipelineService.findById(id) ;
		costPipelineService.delete(pipeline) ;
		
		mv.addObject("location", "admin/cost/pipeline_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
	@RequestMapping("/cylinder_del/{id}")
	public ModelAndView cylinder_del(@PathVariable Integer id){
		ModelAndView mv=new ModelAndView();
		
		CostCylinder cylinder = costCylinderService.findById(id) ;
		costCylinderService.delete(cylinder) ;
		
		mv.addObject("location", "admin/cost/cylinder_list/page/-1");
		mv.setViewName("/admin/cost/redirect") ;
		
		return mv ;
	}
	
}
